package com.cn.icss.controller;

import com.alibaba.fastjson.JSONObject;
import com.cn.icss.service.IStreamService;
import com.cn.icss.service.impl.StreamServiceImpl;
import com.cn.icss.validator.StreamValidator;
import com.cn.icss.vo.StreamInfo;
import com.cn.icss.vo.common.ResponseCode;
import com.cn.icss.vo.common.ResponseResult;
import com.cn.icss.vo.common.ResponseStatus;
import com.jfinal.aop.Before;
import com.jfinal.aop.Inject;
import com.jfinal.core.Controller;
import com.jfinal.ext.interceptor.POST;
import com.jfinal.plugin.redis.Cache;
import com.jfinal.plugin.redis.Redis;
import live.autu.plugin.jfinal.swagger.annotation.Api;
import live.autu.plugin.jfinal.swagger.annotation.ApiImplicitParam;
import live.autu.plugin.jfinal.swagger.annotation.ApiImplicitParams;
import live.autu.plugin.jfinal.swagger.annotation.ApiOperation;
import live.autu.plugin.jfinal.swagger.config.RequestMethod;
import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;

import java.util.HashMap;
import java.util.Map;

/**
 * @中软华腾软件系统有限公司,未经授权不得擅自传播
 * @类描述:
 * @类名称:StreamController
 * @作者: Administrator
 * @编辑时间: 2020/04/08 12:55
 * @版本: 1.0
 */
@Api(description = "流管理接口")
public class StreamController extends Controller {

    @Inject(StreamServiceImpl.class)
    private IStreamService streamService;
    Logger logger = Logger.getLogger(StreamController.class);

    // 消灭前面一大堆，直接写路径覆盖掉前面设置的basePath+viewPath
    @Before(StreamValidator.class)
    @ApiOperation(hidden = true)
    public void index(){
        // 获取名称为“usign”模块的Redis服务
        Cache userCache = Redis.use("liveStream");
        // setex() 可以设置过期时间，无需设置则可使用set()
        userCache.setex("test",3600,"我就是一个测试");
        userCache.set("k","v");
        String hu = userCache.get("test");
        String mu = userCache.get("k");
        System.out.println(mu+","+hu);
        renderText("测试首页");
    }

    /**
    *@中软华腾软件系统有限公司,未经授权不得擅自传播
    *@方法描述: 查询流播放与推送地址
    *@方法参数描述:
    ** @param
    *@作者: 蒋正午
    *@编辑时间: 2020/04/20  10:45
    *@返回值描述: void
    */
    @ApiOperation(methods= RequestMethod.POST)
    @ApiImplicitParams({
            @ApiImplicitParam(name="streamId",required=true, description="流id"),
            @ApiImplicitParam(name="tokenId",required=false, description="登录的sessionId")
    })
    @Before({POST.class,StreamValidator.class})
    public void findStreamAddr(){
        // 验证通过正常开发逻辑, 1. 利用streamId查询相关地址 2. 保存TokenId(也就是SessionId)
        String streamId = getPara("streamId");
        String tokenId = getPara("tokenId");
        // 目前不需要任何认证，所以不需要再保存TokenId了
        if(StringUtils.isBlank(tokenId)){
            tokenId = "";
        }
        Map<String, Object> streamAddr = streamService.findStreamAddr(streamId, tokenId);
        ResponseResult reponse = ResponseResult.build(ResponseCode.OK, "查询播流推流地址成功", ResponseStatus.SUCCESS, streamAddr);
        renderJson(JSONObject.toJSONString(reponse));
    }


    /**
    *@中软华腾软件系统有限公司,未经授权不得擅自传播
    *@方法描述: 查询流的总的信息
    *@方法参数描述:
    ** @param
    *@作者: 蒋正午
    *@编辑时间: 2020/04/20  10:45
    *@返回值描述: void
    */
    @ApiOperation(methods= RequestMethod.POST)
    @ApiImplicitParams({
            @ApiImplicitParam(name="streamId",required=true, description="流id")
    })
    @Before({POST.class,StreamValidator.class})
    public void findStreamInfo(){
        String streamId = getPara("streamId");
        Map<String,Object> allStreamInfo = streamService.findStreamInfo(streamId);
        ResponseResult responseResult = ResponseResult.ok(allStreamInfo);
        renderJson(JSONObject.toJSONString(responseResult));
    }

    /**
    *@中软华腾软件系统有限公司,未经授权不得擅自传播
    *@方法描述: 查询流的最基本的信息
    *@方法参数描述:
    ** @param
    *@作者: 蒋正午
    *@编辑时间: 2020/04/20  10:51
    *@返回值描述: void
    */
    @ApiOperation(methods= RequestMethod.POST)
    @ApiImplicitParams({
            @ApiImplicitParam(name="streamId",required=true, description="流id")
    })
    @Before({POST.class,StreamValidator.class})
    public void findBaseStreamInfo(){
        Map<String,Object> baseStreamInfo = new HashMap<>();
        String streamId = getPara("streamId");
        StreamInfo streamInfo = streamService.findBaseStreamInfo(streamId);
        ResponseResult reponse = ResponseResult.ok(streamInfo);
        renderJson(JSONObject.toJSONString(reponse));
    }

    @ApiOperation(methods = RequestMethod.POST)
    @ApiImplicitParams({
            @ApiImplicitParam(name="roomTypeId",required=true, description="直播间类型id"),
            @ApiImplicitParam(name="dicId",required=true, description="注册平台id")
    })
    @Before({POST.class,StreamValidator.class})
    public void createStream(){
        String roomTypeId = getPara("roomTypeId");
        String dicId = getPara("dicId");
        Map<String,Object> resultMap = streamService.createStream(roomTypeId, dicId);
        ResponseResult responseResult = ResponseResult.ok(resultMap);
        renderJson(JSONObject.toJSONString(responseResult));
    }


    @ApiOperation(methods = RequestMethod.POST)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "streamId",required = true,description = "流id"),
            @ApiImplicitParam(name="roomTypeId",required=true, description="直播间类型id"),
            @ApiImplicitParam(name="newdcid",required=true, description="身份证或者电话号码代表的字典")
    })
    @Before({POST.class})
    public void updateStream(){
        String streamId = getPara("streamId");
        String roomTypeId = getPara("roomTypeId");
        String newdicId = getPara("newdcid");
        Map<String, Object> map = streamService.updateStream(streamId, roomTypeId, newdicId);
        ResponseResult responseResult = ResponseResult.ok(map);
        renderJson(JSONObject.toJSONString(responseResult));
    }



}
